# -*- coding: utf-8 -*-

import time
import os
import sched

#-------------project bag---------------
import multithreading
from log import logger
from tools import *



TimerStatus = 1
Event = []


# --------------------join Timer---------------------------

def joinTimer_CountDown(func,sec=0,args=()):
	global Event
	task = {}
	task['func'] = func
	task['args'] = args
	task['sec'] = int(sec)
	task['status'] = 1
	task['type'] = 'countdown'
	Event.append(task)

def joinTimer_DateTimePoint(func,year,month,day,hour,minute,seconds,args=()):
	global Event
	task = {}
	task['func']=func
	task['args']=args
	task['dt'] = [int(year),int(month),int(day),int(hour),int(minute),int(seconds)]
	task['status'] = 1
	task['type']='datetime'
	Event.append(task)


def joinTimer_TimePoint(func,hour,minute,seconds,args=(),repeat=0):
	global Event
	task = {}
	task['func']=func
	task['args']=args
	task['t'] = [int(hour),int(minute),int(seconds)]
	task['status'] = 1
	task['type']='time'
	Event.append(task)

def joinTimer_DatePoint(func,year,month,day,args=()):
	global Event
	task = {}
	task['func']=func
	task['args']=args
	task['d'] = [int(year),int(month),int(day)]
	task['status'] = 1
	task['type']='date'
	Event.append(task)


# --------------------Timer handle---------------------------

def _RunCountDown():
	global Event
	s = sched.scheduler(time.time, time.sleep)
	for e in Event:
		if not e['type'] == 'countdown':
			continue
		if e['status'] == 1:
			s.enter(e['sec'], 1, e['func'], e['args'])
			e['status'] = 0
	s.run()

def _RunDateTimePoint():
	global Event
	now = date_now()
	for e in Event:
		if not e['type'] == 'datetime':
			continue
		if e['status'] == 1:
			if datetime_diff(e['dt'],now) <= 1:
				multithreading.to_threading(e['func'],e['args'])
				e['status'] = 0

def _RunTimePoint():
	global Event
	now = time_now()
	now_list = [int(now.hour),int(now.minute),int(now.second)]
	for e in Event:
		if not e['type'] == 'time':
			continue
		if e['status'] == 1:
			if time_diff(e['t'],now_list) <= 1  and datetime_diff(e['t'],now_list) >= -1:
				multithreading.to_threading(e['func'],e['args'])
				e['status'] = 0
		if e['repeat'] == 1:
			if e['status'] == 0:
				if time_diff(e['t'],now_list) < -1:
					e['status'] = 1 


def _RunDatePoint():
	global Event
	now = time_now()
	now_list = [int(now.year),int(now.month),int(now.day)]
	for e in Event:
		if not e['type'] == 'date':
			continue
		if e['status'] == 1:
			if time_diff(e['d'],now_list) == 0:
				multithreading.to_threading(e['func'],e['args'])
				e['status'] = 0

# --------------------run Timer---------------------------

def _Check_Event():
	_RunCountDown()      #倒数
	_RunDateTimePoint()
	_RunTimePoint()
	_RunDatePoint()
	

def _DailyCheck():
	global TimerStatus
	print 'beginning====================================='
	logger.info(u'项目开始运行.....')
	current_time = time.ctime()
	logger.info(u'当前时间是:%s' % (current_time))
	while True:
		if TimerStatus == 0:
			logger.info(u'计时器结束')
			break
		_Check_Event()
		# logger.info(u'监测计时器')
		time.sleep(5)


def set_timer():
	multithreading.to_threading(_DailyCheck)

def close_timer():
	global TimerStatus
	TimerStatus = 0

    

